﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace ReadDogv1
{
    public class CsHansdog
    {

        // 定义导入的DLL名称
        const string BurnDogDll = "BurnDog.dll";

        // 导入检测主狗函数
        [DllImport(BurnDogDll, CallingConvention = CallingConvention.StdCall)]
        public static extern int Check();

        // 导入烧录子狗函数
        [DllImport(BurnDogDll, CallingConvention = CallingConvention.StdCall)]
        public static extern int BurnDog([MarshalAs(UnmanagedType.LPStr)] string pcSubDogSN);

        // 导入获取激活码函数
        [DllImport(BurnDogDll, CallingConvention = CallingConvention.StdCall)]
        public static extern int GetActivationCode(
            [MarshalAs(UnmanagedType.LPStr)] StringBuilder pcSubDogID,
            ref int nSubDogIDLen,
            [MarshalAs(UnmanagedType.LPStr)] StringBuilder pcActivationCode,
            ref int nActivationCodeLen
        );

        public static string F_CheckDogExist()
        {
            var ret = "未存在";
            try
            {
                ret = Check() == 0 ? "存在" : "未存在";
            }
            catch (Exception ex)
            {

                MessageBox.Show($"{ex.Message}");
            }
            return ret;
        }

        /// <summary>
        /// 烧录狗
        /// </summary>
        /// <param name="物料编码">给DLL用</param>
        /// <param name="规格型号">写入注册信息，传入DMS</param>
        /// <param name="任务单ID">写入注册信息，传入DMS</param>
        /// <param name="注册信息">写入注册信息，传入DMS</param>
        /// <returns></returns>
        public static bool F_CreateDogInfo(string 物料编码, string 规格型号, string 任务单ID, ref string 注册信息)
        {
            if (BurnDog(物料编码) != 0)
            {
                ViewModel.GetInstance().log.DebugLog($"---BurnDog 函数失败");
                return false;//烧录失败
            }

            //烧录成功
            var 加密狗ID = "";
            var 激活码 = "";

            if (!F_ReadDogInfo(ref 加密狗ID, ref 激活码))
            {
                ViewModel.GetInstance().log.DebugLog($"---GetActivationCode 函数失败，加密狗ID：{加密狗ID}，激活码：{激活码}");
                return false;//烧录失败
            }

            var 激活码List = 激活码.Split(';');
            if (激活码List.Count() < 3)
            {
                ViewModel.GetInstance().log.DebugLog($"---激活码 解析失败，加密狗ID：{加密狗ID}，激活码：{激活码}");
                return false;
            }

            var js = new
            {
                物料编码 = 物料编码,
                任务单ID = 任务单ID,
                规格型号 = 规格型号,
                加密狗ID = 加密狗ID,
                设备启动激活码 = 激活码List[0],
                设备终止激活码 = 激活码List[1],
                服务完成激活码 = 激活码List[2],
            };
            注册信息 = JsonConvert.SerializeObject(js);
            return true;
        }


        public static bool F_ReadDogInfo(ref string 加密狗ID, ref string 激活码)
        {
            StringBuilder subDogID = new StringBuilder(255); // 根据实际情况设置合适的长度
            StringBuilder activationCode = new StringBuilder(255); // 根据实际情况设置合适的长度
            int subDogIDLen = subDogID.Capacity;
            int activationCodeLen = activationCode.Capacity;

            int getCodeResult = GetActivationCode(subDogID, ref subDogIDLen, activationCode, ref activationCodeLen);
            if (getCodeResult != 0)
                return false;

            加密狗ID = subDogID.ToString();
            激活码 = activationCode.ToString();
            return true;
        }
    }
}
